---
title: "Zipkin Branve & Spring Sleuth"
date: 2019-10-18
categories:
- java
tags:
---

<div id="content">
<pre class="example">
https://www.e4developer.com/2018/02/09/tracing-messages-in-choreography-with-sleuth-and-zipkin/
</pre>
<p>
One of the challenges in building distributed system is having a good visibility of what is happening inside them. This challenge is only magnified when dealing with choreography- microservices, loosely coupled, communicating via messaging. In this article you will see how Sleuth and Zipkin help to solve that problem.
</p>
<p>
One of the most important requirements for production ready microservices is being able to correlate logs. What does that mean? Having some sort of id, that will link logs from different services together. Of course you don’t want to link everything- you want to focus on a single request/process that is happening in the system. This was often done with MDC (Mapped Diagnostic Context) in slf4j. There is nothing wrong in using these technologies directly, but here I want to show you something better…
Meet Spring Cloud Sleuth
</p>
<p>
Spring Cloud Sleuth is a project designed to make tracing requests in microservices easy. It succeeds spectacularly in that goal. If you are using Spring Boot (and you should!) enabling Sleuth only requires adding a single dependency:
</p>
<div class="org-src-container">
<pre class="src src-xml">&lt;<span style="font-weight: bold;">dependency</span>&gt;
    &lt;<span style="font-weight: bold;">groupId</span>&gt;org.springframework.cloud&lt;/<span style="font-weight: bold;">groupId</span>&gt;
    &lt;<span style="font-weight: bold;">artifactId</span>&gt;spring-cloud-starter-sleuth&lt;/<span style="font-weight: bold;">artifactId</span>&gt;
&lt;/<span style="font-weight: bold;">dependency</span>&gt;
</pre>
</div>
<p>
After adding this dependency, requests to your microservices will be traced. To see more of that tracing, you need to add the following to your application config:
</p>
<pre class="example">
logging.level.org.springframework.cloud.sleuth=DEBUG
</pre>
<p>
After enabling that, you should start seeing some new, interesting logs in your services:
</p>
<pre class="example">
2018-02-08 22:30:16.431 DEBUG [food-order-publisher,,,] 12572 --- [nio-8080-exec-7] o.s.c.sleuth.instrument.web.TraceFilter : Received a request to uri [/order] that should not be sampled [false]
2018-02-08 22:30:16.454 DEBUG [food-order-publisher,888114b702f9c3aa,888114b702f9c3aa,true] 12572 --- [nio-8080-exec-7] o.s.c.sleuth.instrument.web.TraceFilter : No parent span present - creating a new span
2018-02-08 22:30:16.456 DEBUG [food-order-publisher,888114b702f9c3aa,888114b702f9c3aa,true] 12572 --- [nio-8080-exec-7] o.s.c.s.i.web.TraceHandlerInterceptor : Handling span [Trace: 888114b702f9c3aa, Span: 888114b702f9c3aa, Parent: null, exportable:true]
2018-02-08 22:30:16.457 DEBUG [food-order-publisher,888114b702f9c3aa,888114b702f9c3aa,true] 12572 --- [nio-8080-exec-7] o.s.c.s.i.web.TraceHandlerInterceptor : Adding a method tag with value [orderFood] to a span [Trace: 888114b702f9c3aa, Span: 888114b702f9c3aa, Parent: null, exportable:true]
</pre>
<blockquote>
<p>
<a href="http://e4developer.com/2018/01/28/setting-up-rabbitmq-with-spring-cloud-stream/">http://e4developer.com/2018/01/28/setting-up-rabbitmq-with-spring-cloud-stream/</a>
<a href="http://e4developer.com/2018/02/05/handling-bad-messages-with-rabbitmq-and-spring-cloud-stream/">http://e4developer.com/2018/02/05/handling-bad-messages-with-rabbitmq-and-spring-cloud-stream/</a>
</p>
</blockquote>
<p>
I am using here <a href="https://github.com/bjedrzejewski/food-order-publisher">https://github.com/bjedrzejewski/food-order-publisher</a> project as an example. If you are interested how messaging works and in Spring Cloud Stream, check my earlier post about it. There is another blog post that explains error handling used in the code that we will use here. Now, assuming you have the basics, lets look closer at the log that is being created:
</p>
<pre class="example">
[food-order-publisher,888114b702f9c3aa,888114b702f9c3aa,true]
</pre>
<p>
What you are seeing there, are the respective parameters:
</p>
<p>
appname – the name of the application that logged the span
traceId – the id of the latency graph that contains the span
spanId – the id of a specific operation
exportable – whether the log should be exported to Zipkin or not (more about Zipkin later)
</p>
<p>
Here, the traceId is the same as spanId, because this is the beginning of a trace:
</p>
<pre class="example">
2018-02-08 22:30:16.454 DEBUG [food-order-publisher,888114b702f9c3aa,888114b702f9c3aa,true] 12572 --- [nio-8080-exec-7] o.s.c.sleuth.instrument.web.TraceFilter : No parent span present - creating a new span
</pre>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-10-18</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
